import axios from "axios";
import {useUserStore} from "@/stores"
import type {
    CreateAxiosDefaults,
    AxiosInstance,
    InternalAxiosRequestConfig,
    AxiosResponse,
    AxiosRequestConfig,
    AxiosRequestHeaders
} from "axios";
type ResponseDataDefault = { code: number; data: any } & Record<string, any>;


export default class HTTP<T = unknown> {
    public config: CreateAxiosDefaults<T> | undefined;
    public install: AxiosInstance;
    constructor(config: CreateAxiosDefaults<T> | undefined) {
        this.config = config;
        this.install = axios.create(this.config);
        this.init();
    }
    // 统一请求
    public request<P, R = ResponseDataDefault>(config: AxiosRequestConfig<P> = {}): Promise<AxiosResponse<R, P>> {
        return this.install(config);
    }
    // 添加拦截器
    protected init(): void {
        this.install.interceptors.response.use(
            // @ts-ignore
            (response: AxiosResponse<D, T>) => {
                const $store = useUserStore()
                if (response.status === 200) {
                    if (response.headers['new-token']) {
                        // 设置新的token
                        $store.token = response.headers['new-token']
                    }
                    return Promise.resolve(response);
                }
                return Promise.reject(response);
            },
            (err) => {
                const $store = useUserStore()
                if(err.response.status == 401){
                    window.$message.error(err.response.data.msg + "，请重新登陆")
                    localStorage.removeItem("token")
                    $store.logOut(1)
                }
                else if(err.response.status == 403){
                    window.$message.error(err.response.data.msg + "暂无权限!")
                }
                console.log("服务器错误!");
                return Promise.reject(err);
            }
        );
    }
    public post<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            method: "POST",
            data,
            headers,
            ...config
        });
    }
    public put<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            data,
            method: "PUT",
            headers,
            ...config
        });
    }

    public delete<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        data?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            data,
            method: "DELETE",
            headers,
            ...config
        });
    }
    public get<R = ResponseDataDefault, P = Record<string, any>>(
        url: string,
        params?: P,
        headers?: AxiosRequestHeaders,
        config?: AxiosRequestConfig
    ) {
        return this.request<P, R>({
            url,
            params,
            method: "GET",
            headers,
            ...config
        });
    }
}

export enum HttpMethods {
    GET = "get",
    POST = "post",
    PUT = "put",
    DELETE = "delete"
}